package engine;

/**
 * 
 * A 2D angle represented by two vectors and a position vector. This will be used for
 * subdividing polygons into their triangular regions. A Collection of Angles is 
 * sortable by cross product.
 * 
 * @author Dylan Swiggett
 *
 */
public class Angle implements Comparable{
	public Vector v1, v2;	//The two vectors that form the angle
	public Vector p;		//The position of the angle on the 2D plane
	
	public Angle previousAngle, nextAngle;
	
	private double crossProduct;
	
	public boolean removed;	//Used as part of the triangle subdivision process.
	
	public Angle(Vector v1, Vector v2, Vector p){
		this.v1 = v1;
		this.v2 = v2;
		this.p = p;
		
		this.crossProduct = v1.crossProductZ(v2);
		removed = false;
	}
	
	/**
	 * 
	 * Return the angle converted into a Triangle.
	 * 
	 * @return
	 */
	public Triangle toTriangle(){
		p.print();
		v1.print();
		v2.print();
		return new Triangle(p, p.add(v1), p.add(v2));
	}
	
	public double getCrossProduct(){
		return crossProduct;
	}

	/**
	 * Check if the cross product of the angle is greater or less. If they are the same,
	 * instead sort by a hash of the angle's position.
	 */
	public int compareTo(Object arg0) {
		Angle otherAngle = (Angle) arg0;
		if (crossProduct > otherAngle.getCrossProduct()){
			return 1;
		} else if (crossProduct < otherAngle.getCrossProduct()){
			return -1;
		} else {
			return 0;
		}
	}
	
	/**
	 * Sets the cross product to -1 so it will be ignored in the triangle subdivision process.
	 * What this loses in memory is more than made up for in speed.
	 */
	public void fastRemove(){
		removed = true;
	}
}
